#include "ChatSocket.h"
#include "../ChatBuffer.h"

ChatSocket::ChatSocket(const WS::TcpSocket &tcpSocket)
    : m_TcpSocket(tcpSocket)
{
}

void ChatSocket::Run()
{
    LOGI("chat socket start %s %d", m_TcpSocket.IP(), m_TcpSocket.Port());

    while (1)
    {
        ChatBuffer buf;
        int len = m_TcpSocket.Recv<ChatBuffer>(buf);
        LOGI("len is %d", len);

        if (len > 0)
        {
            ChatServlet(buf);
            
            // LOGI("recv %s ", buf.buf);
        }
        else
        {
            if (len == 0)
            {
                LOGI("len==0 the client close and the task End");
                LOGI("the client %s %d is end", m_TcpSocket.IP(), m_TcpSocket.Port());

                m_TcpSocket.Close();
                break;
            }
            else
            {
                LOGI("len < 0 the client close and the task End");
                LOGI("the client %s %d Get an Error", m_TcpSocket.IP(), m_TcpSocket.Port());
                m_TcpSocket.Close();
                break;
            }
        }
    }
}
void ChatSocket::ChatServlet(const ChatBuffer &req)
{
    LOGI("get a request %s", req.buf);

};